Outputs Final Text COde // XIAO RP2040 — LED on D7 driven linearly by A3 (dark=200, bright=4000) const int SENSOR = A3; const int LED_PIN = D7; // ---- your calibration ---- const int RAW_DARK = 200; // reading at darkest const int RAW_BRIGHT = 4000; // reading at brightest // ---- LED output (12-bit PWM) ---- const int PWM_RANGE = 4095; // do not change int PWM_CAP = 100; // max brightness (0..4095) — raise if too dim int PWM_FLOOR = 0; // small floor (e.g., 2–6) if you want a faint glow at "dark" bool LED_ACTIVE_HIGH = true; // set false if wired active-low // smoothing (optional) const float EMA_ALPHA = 0.15f; // 0..1 (higher = snappier) float ema = 0; static inline float clamp01(float x){ return x < 0 ? 0 : (x > 1 ? 1 : x); } void setup() { Serial.begin(115200); analogReadResolution(12); // high-res PWM analogWriteResolution(12); analogWriteRange(PWM_RANGE); analogWriteFreq(1500); pinMode(LED_PIN, OUTPUT); // seed smoothing ema = analogRead(SENSOR); Serial.println("Linear A3 -> LED (dark=200, bright=4000)"); } void loop() { int raw = analogRead(SENSOR); // smooth a bit so it doesn't jitter ema = (1.0f - EMA_ALPHA)*ema + EMA_ALPHA*raw; // linear normalize: RAW_DARK..RAW_BRIGHT -> 0..1 float n = (ema - RAW_DARK) / float(RAW_BRIGHT - RAW_DARK); n = clamp01(n); // scale to (very) dim PWM range int pwm = int(n * PWM_CAP + 0.5f); if (pwm < PWM_FLOOR) pwm = PWM_FLOOR; if (pwm > PWM_CAP) pwm = PWM_CAP; if (!LED_ACTIVE_HIGH) pwm = PWM_RANGE - pwm; analogWrite(LED_PIN, pwm); // debug (optional) static uint32_t t0=0; if (millis() - t0 > 250) { t0 = millis(); Serial.print("A3 "); Serial.print(int(ema)); Serial.print(" n "); Serial.print(n,3); Serial.print(" pwm "); Serial.println(pwm); } delay(8); }